/*************************************************************************
    > File Name: client.cpp
    > Author: Lancer
    > Mail: 18826075768@163.com
************************************************************************/

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

using namespace std;

int main(){
    pid_t pid = fork();
    if(pid < 0){
        perror("fork error\n");
        return 0;
    }
    else if(pid > 0){
        printf("Parent process\n");
        int status = -1;
        pid_t pr = wait(&status);
        if(WIFEXITED(status)){
            printf("the child porcess %d exit normally.\n", pr);
            printf("the return code is %d.\n", WEXITSTATUS(status));
        }
        else{
            printf("the child process %d exit abnormally.\n", pr);
        }
    }
    else if(pid == 0){
        sleep(5);
        printf("Sub process, PID: %u, PPID: %u\n", getpid(), getppid());
        sleep(5);
        exit(3);
    }
    return 0;
}